package com.sql.azarias.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.sql.azarias.R;
import com.sql.azarias.entity.LiveEntity_;
import com.sql.azarias.player.SuperVideoDetailsActivity;
import com.sql.azarias.uitls.FrescoUitls;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/15.
 */

public class
LiveeAdapter extends RecyclerView.Adapter{
    /**
     * 四种类型
     */
    public static final int BANNER = 0;
    public static final int ENTRANCE = 1;
    public static final int PARTITIONSTITLE = 2;
    public static final int PARTITIONS = 3;
    /**
     * banner集合数据
     */
    private LiveEntity_ liveList ;
    /**
     * 传入上下文
     */
    private  Context context;
    /**
     * 初始化布局
     */
    private  LayoutInflater mLayoutInflater;

    /**
     *
     * @param liveList
     * @param context
     * @param
     */

    public LiveeAdapter(LiveEntity_ liveList, Context context) {
        this.liveList = liveList;
        this.context = context;
        this.mLayoutInflater = LayoutInflater.from(context);
    }
    public int getSpanSize(int pos) {

        int viewType = getItemViewType(pos);
        switch (viewType) {

            case BANNER:
                return 2;
            case PARTITIONSTITLE:
                return 2;
            case PARTITIONS:
                return 1;
            case ENTRANCE:
                return 2;
        }
        return 0;
    }
    /**
     * 相当于getView创建ViewHolder布局
     *
     * @param parent
     * @param viewType 当前的类型
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case BANNER:
                return new BannerViewHolder(mLayoutInflater.inflate(R.layout.item_live_banner,parent,false));
            case ENTRANCE:
                return new EntranceViewHolder(mLayoutInflater.inflate(R.layout.item_live_entrance,parent,false));
            case PARTITIONSTITLE:
                return new PartionsTitleViewHolder(mLayoutInflater.inflate(R.layout.item_live_partition_title,parent,false));
            case PARTITIONS:
                return new PartionsViewHolder(mLayoutInflater.inflate(R.layout.item_live_partition,parent,false));
        }

        return null;
    }

    /**
     * 相当于getView中的绑定数据模块
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(getItemViewType(position)==BANNER){
            //头部banner
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            List<LiveEntity_.DataBean.BannerBean> banerList = liveList.getData().getBanner();
            bannerViewHolder.setData(banerList);
        }else if (getItemViewType(position)==ENTRANCE){
            //本地佈局

        } else if(getItemViewType(position)==PARTITIONSTITLE){
            //直播标题
            PartionsTitleViewHolder partionsTitleViewHolder = (PartionsTitleViewHolder)holder;
            LiveEntity_.DataBean.PartitionsBean partitionsBean = liveList.getData().getPartitions().get(getItemTitle(position));
            //Fresco加载网络图片
            FrescoUitls.showUrl(partionsTitleViewHolder.item_live_partition_icon,partitionsBean.getPartition().getSub_icon().getSrc());
            partionsTitleViewHolder.item_live_partition_title.setText(partitionsBean.getPartition().getName());
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder(
                    "当前" +partitionsBean.getPartition().getCount() + "个直播");
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(
                    context.getResources().getColor(R.color.colorAccent));
            stringBuilder.setSpan(foregroundColorSpan, 2,
                    stringBuilder.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            partionsTitleViewHolder.item_live_partition_count.setText(stringBuilder);
        }else {
            //直播列表
            PartionsViewHolder partionsViewHolder = (PartionsViewHolder) holder;
          final   LiveEntity_.DataBean.PartitionsBean.LivesBeanX livesBeanX = liveList.getData().getPartitions().get(getItemTitle(position)).getLives().get(getItem(position));
            //Fresco加载网络图片
            FrescoUitls.showUrl(partionsViewHolder.item_live_cover,livesBeanX.getCover().getSrc());

            partionsViewHolder.item_live_cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent liveIntent = new Intent(context,SuperVideoDetailsActivity.class);
                    liveIntent.putExtra("isLive",true);
                    liveIntent.putExtra("url",livesBeanX.getPlayurl());
                    context.startActivity(liveIntent);
                }
            });
            //Fresco加载网络图片
            FrescoUitls.showUrl(partionsViewHolder.item_live_user_cover,livesBeanX.getOwner().getFace());
            partionsViewHolder.item_live_title.setText(livesBeanX.getTitle());
            partionsViewHolder.item_live_user.setText(livesBeanX.getOwner().getName());
            partionsViewHolder.item_live_count.setText(livesBeanX.getOnline()+"");
        }

    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return BANNER;
        }else if (position==1){
            return ENTRANCE;
        } else if(getitemtype(position)){
            return PARTITIONSTITLE;
        }else{
            return PARTITIONS;
        }
    }


    public boolean getitemtype(int position){
        //判断 分类 title 的位置
        if((position+3)%5==0){

            return true;
        }
        return false;
    }
    /**
     * 总共有多少个item
     * @return
     */
    @Override
    public int getItemCount() {
        if(liveList!=null){

            return 2+liveList.getData().getPartitions().size()*5;
        }
        return  0;
    }

    public  int getItemTitle(int position){


        return (position+3)/5-1;
    }

    public int getItem(int position){

        return (position+2)%5;

    }


    public class BannerViewHolder extends RecyclerView.ViewHolder{
        private Banner banner;

        public BannerViewHolder(View itemView) {
            super(itemView);
            banner = (Banner)itemView.findViewById(R.id.banner);
        }
        public void setData(List<LiveEntity_.DataBean.BannerBean> banerList ){
            //得到图片地址的集合
            List<String> imageUrls = new ArrayList<>();
            for (int i = 0; i < banerList.size(); i++) {
                String image = banerList.get(i).getImg();
                imageUrls.add(image);
            }
            banner.setImages(imageUrls).setImageLoader(new GlideImageLoader()).start();

        }




        public class GlideImageLoader extends ImageLoader {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                //Glide 加载图片简单用法
                Glide.with(context).load(path).into(imageView);

            }
        }
    }

    public class EntranceViewHolder extends  RecyclerView.ViewHolder{

        public EntranceViewHolder(View itemView) {
            super(itemView);
        }
    }


    {

    }
    public class PartionsTitleViewHolder extends  RecyclerView.ViewHolder{
        private SimpleDraweeView item_live_partition_icon;
        private TextView item_live_partition_title,item_live_partition_count;

        public PartionsTitleViewHolder(View itemView) {
            super(itemView);
            item_live_partition_icon = (SimpleDraweeView)itemView.findViewById(R.id.item_live_partition_icon);
            item_live_partition_title = (TextView) itemView.findViewById(R.id.item_live_partition_title);
            item_live_partition_count = (TextView) itemView.findViewById(R.id.item_live_partition_count);

        }

    }
    public class PartionsViewHolder  extends  RecyclerView.ViewHolder{
        private SimpleDraweeView item_live_cover;
        private TextView item_live_title,item_live_user,item_live_count;
        SimpleDraweeView item_live_user_cover;

        public PartionsViewHolder(View itemView) {
            super(itemView);
            item_live_cover = (SimpleDraweeView)itemView.findViewById(R.id.item_live_cover);
            item_live_user_cover = (SimpleDraweeView)itemView.findViewById(R.id.item_live_user_cover);
            item_live_title = (TextView)itemView.findViewById(R.id.item_live_title);
            item_live_user = (TextView)itemView.findViewById(R.id.item_live_user);
            item_live_count = (TextView)itemView.findViewById(R.id.item_live_count);

        }
    }
}
